package com.example.liuliang.zyfypt_210ll.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.liuliang.zyfypt_210ll.R;
import com.example.liuliang.zyfypt_210ll.bean.LoginBean;
import com.example.liuliang.zyfypt_210ll.bean.SessionIDBean;
import com.example.liuliang.zyfypt_210ll.iface.LoginListener;
import com.example.liuliang.zyfypt_210ll.model.LoginModel;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener,LoginListener{

    private EditText user,password;//用户名密码
    private Button login,register;//登录注册按钮
    private LoginModel loginModel;
    private SessionIDBean sessionIDBean;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        //String msg="hello";
        //Toast.makeText(this, "这是一个测试Toast", Toast.LENGTH_SHORT).show();
        //Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    private void init(){
        user=(EditText)findViewById(R.id.editText);
        password=(EditText)findViewById(R.id.editText2);
        login=(Button)findViewById(R.id.button_login);
        register=(Button)findViewById(R.id.button_regactivity);
        //注册监听器
        login.setOnClickListener(this);
        register.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_login:
                loginModel=new LoginModel();
                loginModel.getResultList(user.getText().toString(),password.getText().toString(),this);
                break;
            case R.id.button_regactivity:
                Intent intent2=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent2);
                break;
        }
    }

    @Override
    public void onResponse(LoginBean beanList) {
        Editable msg=user.getText();
        Toast.makeText(this, "欢迎"+msg+"使用本软件", Toast.LENGTH_SHORT).show();
        sessionIDBean=new SessionIDBean();
        Toast.makeText(this, sessionIDBean.getSessionIDBean, Toast.LENGTH_SHORT).show();
        sessionIDBean.getSessionIDBean=beanList.getSessionid();
        Intent intent3=new Intent(LoginActivity.this,Homepage.class);
        startActivity(intent3);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
